<template>
  <div class="app-container">
    <template>
      <el-row>
        <el-col :span="24">
          <div class="h_top">
            <div class="search_box">
              <el-form :model="query" :inline="true">
                <el-row :gutter="10">
                  <el-col :xl="3" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">编 号</div>
                      <el-input v-model="query.coding" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">名 称</div>
                      <el-input v-model="query.name" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">类 别</div>
                      <el-select v-model="query.category" size="mini">
                        <el-option v-for="(item, index) in theCategory" :key="index" :label="item" :value="index" size="mini" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">状 态</div>
                      <el-select v-model="query.status" size="mini">
                        <el-option v-for="(item, index) in theStatus" :key="index" :label="item" :value="index" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">设 备</div>
                      <v-select v-model="query.equipment_id" action="eqList" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">负责人</div>
                      <v-select v-model="query.admin_id" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">单 位</div>
                      <el-input v-model="query.unit" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">操 作</div>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="search">查找</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div class="toolBars">
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-tickets" size="mini" @click="exportHandler">导出</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-document-add" size="mini" @click="addHandler">增加</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-delete" :disabled="!selection.length" size="mini" @click="remove">删除</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reloadHandler">刷新</el-button>
              </div>
              <div class="fr">总数：<span>{{total}}</span></div>
            </div>
          </div>
        </el-col>
      </el-row>
    </template>
    <div>
      <el-row>
        <el-col :span="24">
          <el-table v-loading="loading" :data="paramList" size="small" stripe border :header-cell-style="{background:'#eef1f6',color:'#606266'}" style="width: 100%" @selection-change="selectionHandler">
            <el-table-column type="index" label="序号" width="50" align="center" />
            <el-table-column type="selection" width="45" align="center" />
            <el-table-column prop="coding" label="编号" width="150" align="center" />
            <el-table-column prop="name" label="名称" width="150" />
            <el-table-column prop="status" width="100" label="状态" align="center" :formatter="statusFormat" />
            <el-table-column prop="category" width="80" label="类型" align="center" :formatter="paramCategoryFormat" />
            <el-table-column prop="eq.name" label="设备" width="180" align="center" />
            <el-table-column prop="notice_low" label="预警偏低" width="90" align="center" />
            <el-table-column prop="warning_low" label="预警过低" width="90" align="center" />
            <el-table-column prop="notice_high" label="预警偏高" width="90" align="center" />
            <el-table-column prop="warning_high" label="预警过高" width="90" align="center" />
            <el-table-column prop="unit" width="90" align="center" label="单位" />
            <el-table-column fixed="right" label="操作" width="" align="center">
              <template scope="scope">
                <el-button type="primary" size="mini" @click="updateHandler(scope.row.id)">编辑</el-button>
                <el-button type="danger" size="mini" @click="confirmDel(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import API from '@/api/asset/param'
import xlsx from 'xlsx'
import exportMethod from '@/mixins/export'
import VSelect from '@/components/select'
import { theStatus, theCategory } from '@/utils/options'
export default {
  inject: ['reload', 'goUrl', 'delOne', 'delMany', 'getStatusFormat', 'getParamCategoryFormat'],
  components: {
    VSelect
  },
  mixins: [exportMethod],
  data() {
    return {
      loading: false,
      total: 0,
      query: { page: 1, limit: 15 },
      paramList: [],
      selection: [],
      theCategory,
      theStatus
    }
  },
  watch: {},
  created() {
    this.index()
  },
  methods: {
    search() {
      this.query.page = 1
      this.index()
    },
    selectionHandler(selection) {
      this.selection = selection
    },
    paramCategoryFormat(row) {
      return this.getParamCategoryFormat(row)
    },
    index() {
      this.loading = true
      API.paramList(this.query).then(response => {
        this.total = response.data.total
        this.paramList = response.data.data
      }).finally(() => {
        this.loading = false
      })
    },
    exportHandler() {
      const data = [
        [
          '编号',
          '名称',
          '成本中心',
          '类别',
          '状态',
          '设备',
          '预警偏低',
          '预警过低',
          '预警偏高',
          '预警过高',
          '维护计划',
          '单位',
          '负责人',
          '电话'
        ]
      ]
      let status = ''
      let category = ''
      if (this.selection.length === 0) {
        this.$message.error('请选择要到导出的数据')
        return false
      } else {
        this.selection.forEach((item) => {
          if (item.status === 0) {
            status = '测试'
          } else if (item.status === 1) {
            status = '启用'
          } else if (item.status === 2) {
            status = '临时移除'
          } else if (item.status === 3) {
            status = '停用'
          }
          if (item.category === 0) {
            category = '测量'
          } else if (item.category === 1) {
            category = '增量型的'
          } else if (item.category === 2) {
            category = '非增量型'
          }
          data.push([
            item.coding,
            item.name,
            item.cost.coding,
            category,
            status,
            item.eq.name,
            item.notice_low,
            item.warning_low,
            item.notice_high,
            item.warning_high,
            item.plan.name,
            item.unit,
            item.admin.name,
            item.admin.phone
          ])
        })
      }
      const workbook = {
        SheetNames: ['参数数据'],
        Sheets: {
          '参数数据': xlsx.utils.aoa_to_sheet(data)
        }
      }
      this.exportExec(workbook, '参数数据报表.xlsx')
    },
    remove() {
      const selection = this.selection
      this.delMany(API, selection)
    },
    confirmDel(id) {
      this.delOne(API, id, false)
    },
    addHandler() {
      this.goUrl('/special/param/param_add')
    },
    updateHandler(id) {
      const url = `/special/param/param_edit/${id}`
      this.goUrl(url)
    },
    reloadHandler() {
      this.reload()
    },
    statusFormat(row) {
      return this.getStatusFormat(row)
    }
  }
}
</script>
